package com.atbun.eduservice.service.impl;

import com.atbun.eduservice.client.VodClient;
import com.atbun.eduservice.entity.EduVideo;
import com.atbun.eduservice.entity.vo.VideoVo;
import com.atbun.eduservice.mapper.EduVideoMapper;
import com.atbun.eduservice.service.EduVideoService;
import com.atbun.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-06
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    /**
     * 根据章节id查找小节信息
     * @param chapterId
     * @return
     */
    @Override
    public boolean getCountByChapterId(String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        Integer count = this.baseMapper.selectCount(queryWrapper);
        return null != count && count > 0;
    }

//    添加课时
    @Override
    public void saveVideoInfo(VideoVo videoVo) {
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoVo, video);
        boolean result = this.save(video);
        if(!result){
            throw new GuliException(20001, "课时信息保存失败");
        }
    }

    @Override
    public VideoVo getVideoInfoFormById(String id) {
        //从video表中取数据
        EduVideo video = this.getById(id);
        if(video == null){
            throw new GuliException(20001, "数据不存在");
        }

        //创建videoInfoForm对象
        VideoVo videoVo = new VideoVo();
        BeanUtils.copyProperties(video, videoVo);
        return videoVo;
    }

    @Override
    public void updateVideoInfoById(VideoVo videoVo) {
        //保存课时基本信息
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoVo, video);
        boolean result = this.updateById(video);
        if(!result){
            throw new GuliException(20001, "课时信息保存失败");
        }
    }

    @Override
    public boolean removeVideoById(String id) {
        //根据课时id查询云端视频id
        EduVideo eduVideo = baseMapper.selectById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
//        删除视频资源(调用service-vod服务)
        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.deleteVideo(videoSourceId);
        }

        //删除小节内容
        Integer result = baseMapper.deleteById(id);
        return null != result && result > 0;
    }

    /**
     * 根据课程id删除视频信息
     * @param courseId
     * @return
     */
    @Override
    public boolean deleteVideoByCourseId(String courseId) {
        Integer deleteVideoById = this.baseMapper.deleteVideoByCourseId(courseId);
        return null != deleteVideoById && deleteVideoById > 0;
    }

    /**
     * 根据课程id删除多个视频
     * @param courseId
     * @return
     */
    @Override
    public boolean removeByCourseId(String courseId) {
        //根据课程id查询所有视频列表
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.select("video_source_id");
        List<EduVideo> videoList = this.baseMapper.selectList(queryWrapper);

//        得到所有视频列表的云端原视频id
        List<String> videoSourceIdList = new ArrayList<>();
        for (int i =0; i<videoList.size(); i++){
            EduVideo eduVideo = videoList.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){
                videoSourceIdList.add(videoSourceId);
            }
        }

        //调用vod服务删除远程视频
        if (videoSourceIdList.size() > 0){
            vodClient.deleteVideoList(videoSourceIdList);
        }

//        删除video表示的记录
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", courseId);
        Integer count = baseMapper.delete(queryWrapper2);
        return null != count && count > 0;
    }


}
